package DFA;

import org.omg.CORBA.INTERNAL;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;

public class TxtReadIn {
    private int []initialValue = null;
    private HashMap <Integer,ArrayList<Integer>>finalArray;


    public int[] getInitialValue() {
        return initialValue;
    }

    public void setInitialValue(int[] initialValue) {
        this.initialValue = initialValue;
    }

    public HashMap<Integer, ArrayList<Integer>> getFinalArray() {
        return finalArray;
    }

    public void setFinalArray(HashMap<Integer, ArrayList<Integer>> finalArray) {
        this.finalArray = finalArray;
    }

    public TxtReadIn(int DFANumber) {
        initialValue = new int[DFANumber];
        this.finalArray = new HashMap<>(DFANumber);
    }
    public  HashMap<Integer, int[][]> txt2String(File file) {
        StringBuilder stringBuilder = new StringBuilder();
        int[][] results = null;
        int dfaNumber = 0;
        int txtInputValue = 0;
        HashMap<Integer, int[][]> integerHashMap = new HashMap<>();
        try {
            BufferedReader br = new BufferedReader(new FileReader(file));//构造一个BufferedReader类来读取文件
            String s = null;
            int countLine = 0;
            int countFact = 0;
            while ((s = br.readLine()) != null) {//使用readLine方法，一次读一行
                String[] s1 = s.split(" ");
                if(s1[0].contains("DFA")){
                    txtInputValue = Integer.parseInt(s1[1]);
                    results = new int[txtInputValue][3];
                 //   initialValue[countFact] = Integer.parseInt(s1[2]);
                    countFact++;
                    continue;
                }
                if (s1[0].contains("Final")) {
                    ArrayList arrayList = new ArrayList<Integer>();
                    this.finalArray.put((countFact-1),arrayList) ;
                    for (int i = 0; i < s1.length - 1; i++) {
                        arrayList.add(Integer.parseInt(s1[i+1])) ;
                    }
                    continue;
                }

                results[countLine][0] = Integer.parseInt(s1[0]);
                results[countLine][2] = Integer.parseInt(s1[2]);
                results[countLine][1] = Integer.parseInt(s1[1]);
                countLine++;
                if (countLine == txtInputValue) {
                    integerHashMap.put(dfaNumber,results);
                    dfaNumber++;
                    countLine = 0;
                }
                stringBuilder.append(System.lineSeparator() + s);
                if(countFact > initialValue.length) break;
            }

            br.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return integerHashMap;
    }

}
